From ac1d09b5d0dd7e9877ace3954d6042723fa75169 Mon Sep 17 00:00:00 2001
From: Dom Cobley <popcornmix@gmail.com>
Date: Mon, 15 Aug 2022 19:44:20 +0100
Subject: [PATCH] vc04_services/vc-sm-cma: Handle upstream require
 vchiq_instance to be passed around

---
 .../staging/vc04_services/vc-sm-cma/vc_sm.c   | 12 +++----
 .../vc04_services/vc-sm-cma/vc_sm_cma_vchi.c  | 36 +++++++++++--------
 .../vc04_services/vc-sm-cma/vc_sm_cma_vchi.h  |  2 +-
 3 files changed, 28 insertions(+), 22 deletions(-)

--- a/drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
+++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
@@ -106,6 +106,7 @@ struct sm_state_t {
 					 * has finished with a resource.
 					 */
 	u32 int_trans_id;		/* Interrupted transaction. */
+	struct vchiq_instance *vchiq_instance;
 };
 
 struct vc_sm_dma_buf_attachment {
@@ -1491,7 +1492,6 @@ static const struct file_operations vc_s
 static void vc_sm_connected_init(void)
 {
 	int ret;
-	struct vchiq_instance *vchiq_instance;
 	struct vc_sm_version version;
 	struct vc_sm_result_t version_result;
 
@@ -1501,7 +1501,7 @@ static void vc_sm_connected_init(void)
 	 * Initialize and create a VCHI connection for the shared memory service
 	 * running on videocore.
 	 */
-	ret = vchiq_initialise(&vchiq_instance);
+	ret = vchiq_initialise(&sm_state->vchiq_instance);
 	if (ret) {
 		pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
 		       __func__, ret);
@@ -1509,7 +1509,7 @@ static void vc_sm_connected_init(void)
 		return;
 	}
 
-	ret = vchiq_connect(vchiq_instance);
+	ret = vchiq_connect(sm_state->vchiq_instance);
 	if (ret) {
 		pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
 		       __func__, ret);
@@ -1518,7 +1518,7 @@ static void vc_sm_connected_init(void)
 	}
 
 	/* Initialize an instance of the shared memory service. */
-	sm_state->sm_handle = vc_sm_cma_vchi_init(vchiq_instance, 1,
+	sm_state->sm_handle = vc_sm_cma_vchi_init(sm_state->vchiq_instance, 1,
 						  vc_sm_vpu_event);
 	if (!sm_state->sm_handle) {
 		pr_err("[%s]: failed to initialize shared memory service\n",
@@ -1576,7 +1576,7 @@ err_remove_misc_dev:
 	misc_deregister(&sm_state->misc_dev);
 err_remove_debugfs:
 	debugfs_remove_recursive(sm_state->dir_root);
-	vc_sm_cma_vchi_stop(&sm_state->sm_handle);
+	vc_sm_cma_vchi_stop(sm_state->vchiq_instance, &sm_state->sm_handle);
 }
 
 /* Driver loading. */
@@ -1614,7 +1614,7 @@ static int bcm2835_vc_sm_cma_remove(stru
 		debugfs_remove_recursive(sm_state->dir_root);
 
 		/* Stop the videocore shared memory service. */
-		vc_sm_cma_vchi_stop(&sm_state->sm_handle);
+		vc_sm_cma_vchi_stop(sm_state->vchiq_instance, &sm_state->sm_handle);
 	}
 
 	if (sm_state) {
--- a/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c
+++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c
@@ -70,7 +70,7 @@ struct sm_instance {
 	struct list_head free_list;
 
 	struct semaphore free_sema;
-
+	struct vchiq_instance *vchiq_instance;
 };
 
 /* ---- Private Variables ------------------------------------------------ */
@@ -79,11 +79,11 @@ struct sm_instance {
 
 /* ---- Private Functions ------------------------------------------------ */
 static int
-bcm2835_vchi_msg_queue(unsigned int handle,
+bcm2835_vchi_msg_queue(struct vchiq_instance *vchiq_instance, unsigned int handle,
 		       void *data,
 		       unsigned int size)
 {
-	return vchiq_queue_kernel_message(handle, data, size);
+	return vchiq_queue_kernel_message(vchiq_instance, handle, data, size);
 }
 
 static struct
@@ -187,12 +187,12 @@ static int vc_sm_cma_vchi_videocore_io(v
 
 	while (1) {
 		if (svc_use)
-			vchiq_release_service(instance->service_handle[0]);
+			vchiq_release_service(instance->vchiq_instance, instance->service_handle[0]);
 		svc_use = 0;
 
 		if (wait_for_completion_interruptible(&instance->io_cmplt))
 			continue;
-		vchiq_use_service(instance->service_handle[0]);
+		vchiq_use_service(instance->vchiq_instance, instance->service_handle[0]);
 		svc_use = 1;
 
 		do {
@@ -212,7 +212,8 @@ static int vc_sm_cma_vchi_videocore_io(v
 			mutex_unlock(&instance->lock);
 			/* Send the command */
 			status =
-				bcm2835_vchi_msg_queue(instance->service_handle[0],
+				bcm2835_vchi_msg_queue(instance->vchiq_instance,
+						       instance->service_handle[0],
 						       cmd->msg, cmd->length);
 			if (status) {
 				pr_err("%s: failed to queue message (%d)",
@@ -235,7 +236,8 @@ static int vc_sm_cma_vchi_videocore_io(v
 
 		} while (1);
 
-		while ((header = vchiq_msg_hold(instance->service_handle[0]))) {
+		while ((header = vchiq_msg_hold(instance->vchiq_instance,
+						instance->service_handle[0]))) {
 			reply = (struct vc_sm_result_t *)header->data;
 			if (reply->trans_id & 0x80000000) {
 				/* Async event or cmd from the VPU */
@@ -247,7 +249,8 @@ static int vc_sm_cma_vchi_videocore_io(v
 						      header->size);
 			}
 
-			vchiq_release_message(instance->service_handle[0],
+			vchiq_release_message(instance->vchiq_instance,
+					      instance->service_handle[0],
 					      header);
 		}
 
@@ -264,15 +267,16 @@ static int vc_sm_cma_vchi_videocore_io(v
 	return 0;
 }
 
-static enum vchiq_status vc_sm_cma_vchi_callback(enum vchiq_reason reason,
+static enum vchiq_status vc_sm_cma_vchi_callback(struct vchiq_instance *vchiq_instance,
+						 enum vchiq_reason reason,
 						 struct vchiq_header *header,
 						 unsigned int handle, void *userdata)
 {
-	struct sm_instance *instance = vchiq_get_service_userdata(handle);
+	struct sm_instance *instance = vchiq_get_service_userdata(vchiq_instance, handle);
 
 	switch (reason) {
 	case VCHIQ_MESSAGE_AVAILABLE:
-		vchiq_msg_queue_push(handle, header);
+		vchiq_msg_queue_push(vchiq_instance, handle, header);
 		complete(&instance->io_cmplt);
 		break;
 
@@ -320,6 +324,8 @@ struct sm_instance *vc_sm_cma_vchi_init(
 		list_add(&instance->free_blk[i].head, &instance->free_list);
 	}
 
+	instance->vchiq_instance = vchiq_instance;
+
 	/* Open the VCHI service connections */
 	instance->num_connections = num_connections;
 	for (i = 0; i < num_connections; i++) {
@@ -358,7 +364,7 @@ struct sm_instance *vc_sm_cma_vchi_init(
 err_close_services:
 	for (i = 0; i < instance->num_connections; i++) {
 		if (instance->service_handle[i])
-			vchiq_close_service(instance->service_handle[i]);
+			vchiq_close_service(vchiq_instance, instance->service_handle[i]);
 	}
 	kfree(instance);
 err_null:
@@ -366,7 +372,7 @@ err_null:
 	return NULL;
 }
 
-int vc_sm_cma_vchi_stop(struct sm_instance **handle)
+int vc_sm_cma_vchi_stop(struct vchiq_instance *vchiq_instance, struct sm_instance **handle)
 {
 	struct sm_instance *instance;
 	u32 i;
@@ -385,8 +391,8 @@ int vc_sm_cma_vchi_stop(struct sm_instan
 
 	/* Close all VCHI service connections */
 	for (i = 0; i < instance->num_connections; i++) {
-		vchiq_use_service(instance->service_handle[i]);
-		vchiq_close_service(instance->service_handle[i]);
+		vchiq_use_service(vchiq_instance, instance->service_handle[i]);
+		vchiq_close_service(vchiq_instance, instance->service_handle[i]);
 	}
 
 	kfree(instance);
--- a/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.h
+++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.h
@@ -35,7 +35,7 @@ struct sm_instance *vc_sm_cma_vchi_init(
 /*
  * Terminates the shared memory service.
  */
-int vc_sm_cma_vchi_stop(struct sm_instance **handle);
+int vc_sm_cma_vchi_stop(struct vchiq_instance *vchi_instance, struct sm_instance **handle);
 
 /*
  * Ask the shared memory service to free up some memory that was previously
